package cn.dzz.util;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * @author ArkD42
 * @file OA
 * @create 2020 - 06 - 17 - 10:34
 */
public class HikariUtil {

    private static DataSource dataSource;

    static {
        InputStream inputStream = HikariUtil.class.getClassLoader().getResourceAsStream("cn/dzz/config/hikari.properties");
        Properties properties = new Properties();
        try {
            properties.load(inputStream);
            HikariConfig hikariConfig = new HikariConfig(properties);
            dataSource = new HikariDataSource(hikariConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static DataSource getDataSource() {
        return dataSource;
    }

    public static Connection getConnection(){
        try {
            return dataSource.getConnection();
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return null;
    }
}
